<?php

    class Application_Controller_Helper_Signup extends Zend_Controller_Action_Helper_Abstract
    {

        private $view;

        public function preDispatch()
        {
            $this->view = $view = $this->getActionController()->view;
            $messenger = Zend_Controller_Action_HelperBroker::getStaticHelper('FlashMessenger');
            $_redirector = Zend_Controller_Action_HelperBroker::getStaticHelper('redirector');

            $controllerName = Zend_Controller_Front::getInstance()->getRequest()->getControllerName();
            $actionName = Zend_Controller_Front::getInstance()->getRequest()->getActionName();

            //gcm($messenger); die;

            $request = $this->getActionController()->getRequest();

            $this->step = $step = $request->getPost("step");
            $form = new Application_Form_LoginForm(array('step'=>$this->step));
            if ($request->isPost() && $request->getPost('form') == 'signup')
            {
                if ($form->isValid($request->getPost()))
                {
                    $data = $form->getValues();
                    // process data
                    $step = $this->signupProcess($data, $this->step);
                    
                    //if 3 is returned - user successfully registered - remains activation step
                    if($step == 3)
                    {
                        
                        $messenger->addMessage(
                            array('success' => '<strong>Success!</strong> Please now activate your account!')
                        );
                        $_redirector->gotoUrl($controllerName."/".$actionName."?step=3&email=".$this->view->signupemail."&user_id=".$this->view->user_id."&pswa=".$this->view->pswa);
                    }

                    //if 0 is returned - user already registered
                    if ($step == -1)
                    {
                        $messenger->addMessage(
                            array('error' => '<strong>Oops!</strong> Username already used!')
                        );
                        $_redirector->gotoUrl($controllerName."/".$actionName."?email=".$this->view->signupemail."&step=2");
                        $step = 2;
                    }

                    if ($step === 0)
                    {
                        $messenger->addMessage(
                                array('error' => '<strong>Oops!</strong> Your account is already registered!')
                        );
                        $_redirector->gotoUrl($controllerName."/".$actionName);
                    }
                    $view->step = $step;
                    
                    $view->processed = true;
                }
                else
                {
                    $messenger->addMessage(
                            array('error' => '<strong>Oops!</strong> Your account is not activated yet!')
                    );
                    $_redirector->gotoUrl("");
                }
            }

            $view->signupForm = $form;
        }

        public function signupProcess($dataForm, $step)
        {
            $commonModel = new Application_Model_Common;
            switch ($step)
            {
                case '1':
                    $emailAddres = $commonModel->getAllRecordDetail(RKN_USER, " email_address = '" . $dataForm['email_address'] . "' ");
                    if (count($emailAddres))
                    {
                        return 0;
                    }
                    $this->view->signupemail = $dataForm['email_address'];
//                    else
//                    $id = $commonModel->insertRecordData($data_array, RKN_USER);
                    $this->view->step = 2;
                    return 2;
                    break;
                case '2':
                    //check for email address validation
                    $emailAddres = $commonModel->getAllRecordDetail(RKN_USER, " email_address = '" . $dataForm['email_address'] . "' ");
                    if (count($emailAddres))
                    {
                        return 0;
                    }

                    //check for username
                    $username = $commonModel->getAllRecordDetail(RKN_USER, " username = '" . $dataForm['username'] . "' ");
                    $this->view->signupemail = $dataForm['email_address'];
                    //username already exists
                    if (count($username))
                        return -1;
                    
                    $data_array = array(
                        "email_address"=>$dataForm['email_address'],
                        "first_name"=>$dataForm['first_name'],
                        "last_name"=>$dataForm['last_name'],
                        "username"=>$dataForm['username'],
                        "password"=>md5($dataForm['password1']),
                        "zip_code"=>$dataForm['zip_code'],
                        "address"=>$dataForm['address']
                    );
                    $id = $commonModel->insertRecordData($data_array, RKN_USER);
                    $this->view->signupemail = $dataForm['email_address'];
                    $this->view->user_id = getEncryptedString($id);
                    $this->view->pswa = getEncryptedString($dataForm['password1']);
                    return 3;
            }
        }

    }